package main

import (
	"fmt"
	"math/rand"
	"sort"
	"strconv"
	"time"
)

type Student struct {
	Name  string
	Score float64
}

// 结构体切片
type StudentSlice []Student

func (students StudentSlice) Len() int {
	return len(students)
}

func (students StudentSlice) Less(i, j int) bool {
	return students[i].Score > students[j].Score
}

func (students StudentSlice) Swap(i, j int) {
	students[i], students[j] = students[j], students[i]
}

func main() {
	rand.Seed(time.Now().UnixNano())
	var students StudentSlice
	for i := 0; i <= 9; i++ {
		score, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", rand.Float64()*100), 64)
		student := Student{
			Name:  fmt.Sprintf("学生%d", i),
			Score: score,
		}
		students = append(students, student)
	}
	for _, v := range students {
		fmt.Println(v)
	}
	sort.Sort(students)
	fmt.Println("------------------------排序后------------------------")
	for _, v := range students {
		fmt.Println(v)
	}
}
